"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const geoserver_js_1 = require("./util/geoserver.js");
class ImageMosaicClient {
    constructor(url, auth) {
        this.url = url;
        this.auth = auth;
    }
    async getGranules(workspace, coverageStore, coverage) {
        const url = this.url + 'workspaces/' + workspace + '/coveragestores/' +
            coverageStore + '/coverages/' + coverage + '/index/granules.json';
        const response = await fetch(url, {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth,
                'content-type': 'text/plain'
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return response.json();
    }
    async harvestGranules(workspace, coverageStore, filePath) {
        const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/external.imagemosaic';
        const response = await fetch(url, {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth,
                'content-type': 'text/plain'
            },
            body: filePath
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return response.json();
    }
    async addGranuleByServerFile(workspace, coverageStore, filePath) {
        const url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/external.imagemosaic';
        const response = await fetch(url, {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth,
                'content-type': 'text/plain'
            },
            body: filePath
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
    }
    async deleteSingleGranule(workspace, coverageStore, coverage, covFileLocation) {
        let url = this.url + 'workspaces/' + workspace + '/coveragestores/' + coverageStore + '/coverages/' + coverage + '/index/granules.xml';
        url += '?filter=location=\'' + covFileLocation + '\'';
        const response = await fetch(url, {
            credentials: 'include',
            method: 'DELETE',
            headers: {
                Authorization: this.auth,
                'content-type': 'text/plain'
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return true;
    }
}
exports.default = ImageMosaicClient;
